package com.erpanderp.wam.dbmigration.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpenseDetails;
import com.erpanderp.wam.ac.model.entity.reimbursement.TransportExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.dbmigration.model.entity.FlexExpenseTransportation;
import com.erpanderp.wam.dbmigration.model.entity.FlexMiscDetails;
import com.erpanderp.wam.dbmigration.model.entity.FlexMiscMaster;
import com.erpanderp.wam.util.HibernateUtil;

public class MiscellaneousDAO {
	
	public  List<FlexMiscMaster> getFlexMiscExpenseMasterList(){
		List<FlexMiscMaster> flexMiscExpenseMasterList  = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexMiscMaster fmisc";
            Query query = session.createQuery(queryString);
            flexMiscExpenseMasterList = (List<FlexMiscMaster>) query.list();
         
            System.out.println("FLEX FLEX MISCELLANEOUS EXPENSE LIST SIZE IS "+flexMiscExpenseMasterList.size());
            return flexMiscExpenseMasterList;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public  List<FlexMiscDetails> getMiscExpenseDetailsList(){
		List<FlexMiscDetails> flexMiscExpenseDetailsList  = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexMiscDetails fmd";
            Query query = session.createQuery(queryString);
            flexMiscExpenseDetailsList = (List<FlexMiscDetails>) query.list();
         
            System.out.println("FLEX FLEX MISCELLANEOUS EXPENSE DETAILS LIST SIZE IS "+flexMiscExpenseDetailsList.size());
            return flexMiscExpenseDetailsList;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	
	public void createWAMMiscExpenses(List<MiscExpense> wamMiscExpenseList){
		System.out.println("IMPORTING INTO WAM MISC EXPENSE");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for(MiscExpense miscExpense : wamMiscExpenseList){
             session.save(miscExpense);
			}
            session.getTransaction().commit();
          
            
		}catch (Exception e) {
			e.printStackTrace();
			
			// TODO: handle exception
		}
	}
	
	public void createWAMMiscExpensesDetails(List<MiscExpenseDetails> wamMiscExpenseDetailsList){
		System.out.println("IMPORTING INTO WAM MISC EXPENSES DETAILS EXPENSE");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for(MiscExpenseDetails miscExpenseDetails : wamMiscExpenseDetailsList){
             session.save(miscExpenseDetails);
			}
            session.getTransaction().commit();
          
            
		}catch (Exception e) {
			e.printStackTrace();
			
			// TODO: handle exception
		}
	}
	
	
	public MiscExpense getMiscExpenseByExpenseId(String miscExpenseCode){
		MiscExpense miscExpense = null;
		System.out.println("IMPORTING FLEX MIS EXPENSES");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from MiscExpense me where me.miscExpenseCode=:miscExpenseCode";
            Query query = session.createQuery(queryString).setCacheable(true).setString("miscExpenseCode", miscExpenseCode);
            List temp = (List) query.list();
            if(temp != null && temp.size() > 0){
            	miscExpense = (MiscExpense) temp.get(0);
            }
            return miscExpense;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	

}
